/*
  Meanwhile - Unofficial Lotus Sametime client library
  
  Copyright (c) 2007 Christopher O'Brien <siege@preoccupied.net>
  
  License: LGPL
  
  This library is free software; you can redistribute it and/or modify
  it under the terms of version 2 the GNU Library General Public
  License as published by the Free Software Foundation.
                                                                                
  This library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA
*/


#include <glib.h>
#include <glib-object.h>
#include "mw-config.h"
#include "mw-debug.h"
#include "mw-marshal.h"
#include "mw-object.h"


gpointer mw_gobject_ref(gpointer data) {
  if(data) {
    g_object_ref(G_OBJECT(data));

#if DEBUG_REFCOUNT
    g_debug("mw_gobject_ref %p to %u", data, mw_gobject_refcount(data));
#endif
  }
  
  return data;
}


gpointer mw_gobject_unref(gpointer data) {
  if(data) {

#if DEBUG_REFCOUNT
    g_debug("mw_gobject_unref %p from %u", data, mw_gobject_refcount(data));
#endif

    g_object_unref(G_OBJECT(data));
  }

  return NULL;
}


guint mw_gobject_refcount(gpointer data) {
  GObject *gobj;

  g_return_val_if_fail(data != NULL, 0);
  gobj = G_OBJECT(data);

  return (gobj? gobj->ref_count: 0);
}


void mw_gobject_set_weak_pointer(gpointer obj, gpointer *weak) {
  g_return_if_fail(weak != NULL);
  
  mw_gobject_clear_weak_pointer(weak);

  g_debug("setting weak pointer at %p to object %p", weak, obj);

  if(obj) {
    GObject *gobj = G_OBJECT(obj);
    g_object_add_weak_pointer(gobj, weak);
    *weak = gobj;
  }
}


void mw_gobject_clear_weak_pointer(gpointer *weak) {
  g_return_if_fail(weak != NULL);

  if(*weak) {
    g_debug("clearing weak pointer at %p to object %p", weak, *weak);
    g_object_remove_weak_pointer(G_OBJECT(*weak), weak);
    *weak = NULL;
  }  
}


void mw_property_boolean(GObjectClass *gclass,
			 guint property_id,
			 const gchar *name, const gchar *blurb,
			 GParamFlags flags) {
  GParamSpec *gps;
  gps = g_param_spec_boolean(name, NULL, blurb, FALSE, flags);
  g_object_class_install_property(gclass, property_id, gps);
}


/* The end. */
